package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.DishService;
import com.sky.service.ShoppingCartService;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public void deleteShopping() {
        Long userId = BaseContext.threadLocaUserlId.get();
        shoppingCartMapper.deleteByUserId(userId);
    }

    @Override
    public List<ShoppingCart> getShoppingCart() {
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.threadLocaUserlId.get());
        List<ShoppingCart> shoppingCart1 =  shoppingCartMapper.selectByAllshoppingCart(shoppingCart);
        return shoppingCart1;
    }

    /**
     * 添加购物车
     * @param shoppingCartDTO 菜品的ID 用户的id
     */
    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        // 先判断数据库是否有数据 有的话就加一条数据

        // 封装为实体对象
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);

        // 调用Mapper 查询   先获得 用户的ID
        shoppingCart.setUserId(BaseContext.threadLocaUserlId.get());

          List<ShoppingCart> queryShoppingCart = shoppingCartMapper.selectByAllshoppingCart(shoppingCart);
          // 创建一个新对象 处理 数据库内容
          ShoppingCart dbShoppingCart = null;

        //判断是否接收到数据
        if(queryShoppingCart !=null && queryShoppingCart.size() > 0 ){
             //如果有数据说明点过餐  不用加新数据 直接在原本数据 加一份数量即可
            // 因为用的集合 但是条件SQL语句条件 只能查到一份数据
            //为了 多次使用同一动态SQL  用的集合接受 所以数据绝对在 集合 的 第一个 索引
            dbShoppingCart = queryShoppingCart.get(0);

            dbShoppingCart.setNumber(dbShoppingCart.getNumber()+1);
            shoppingCartMapper.updateShoppingCart(dbShoppingCart);
        }else {
            //没有就新增一条  给的数据只有四个 其他数据 自己追加
            //菜品名称  口味 图片 金额 在获得的 ID判断事菜品ID还是 人气ID
            if (shoppingCart.getDishId() !=null ){
                // 查询 菜品的信息
              Dish dish = dishMapper.getDish(shoppingCart.getDishId());

                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
                // 口味信息 在口味表中 获得口味


            }else {
                //当前是套餐 就根据套餐ID获得信息
                SetmealVO setmeal =  setmealMapper.selectByIdSetmeal(shoppingCart.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setImage(setmeal.getImage());

            }
            // 时间 和 数量都是 1
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setNumber(1);
            shoppingCartMapper.insertShoppingCart(shoppingCart);  //插入数据
        }



    }
}
